if exists (select 1 from sysobjects where name = 'set_zeit' and type = 'P')
begin
   drop procedure set_zeit
   print 'Procedure: set_zeit deleted ...'
end
go
create procedure set_zeit(
  @dienstID             int           = 1,
  @von               char(8)       = '20:30:01',--'00000000',
  @bis               char(8)       = '20:30:05',
  @zeiteinheit       char(1)       = 'P',
  @datevon           char(8)       = '20090101',
  @datebis           char(8)       = '20300101',
  @abteilungszeit    int           = 0
)
as
begin
  set nocount on
   if @dienstID = 0 or @dienstID = '' select @dienstID = null




---- delete all that have the same begin date as end date
--  delete from DienstZeit where DienstID = @dienstID and datbis = datvon
--  delete from DienstZeit where DienstID = @dienstID and datvon >= @datevon

--  update DienstZeit
--     set datbis = @datevon
--   where DienstID = @dienstID
--     and datbis >= @datevon
--     --and Von <= @datebis

--select * from DienstZeit

   if (@dienstID is not null)
   begin
      insert into DienstZeit (DienstID, Von, Bis, Zeiteinheit,datvon,datbis,abteilungzeit) 
          values (@dienstID, convert(datetime, @von, 108), convert(datetime, @bis, 108), @zeiteinheit, @datevon,@datebis,@abteilungszeit)
   end
   
   
   
   
   
end
go
print 'Procedure: set_zeit done ...'
go
grant exec on set_zeit to prsadmins with grant option
go
grant exec on set_zeit to prsusers
go

